package com.toolkit.util.excelutil;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelStyleUtil {

    /**
     * 设置Excel样式,字体大小和位置(带边框)
     * @param workbook
     * @param size
     * @param position
     * @return
     */
    public static XSSFCellStyle setExcelStyle(XSSFWorkbook workbook, int size, HorizontalAlignment position) {
        // 创建单元格样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置字体大小和居中
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) size); // 字体大小，注意在XSSF中，字体大小是以半点数为单位的，例如12pt应为24（即12 * 2）
        style.setFont(font);
        style.setAlignment(position); // 水平居中HorizontalAlignment.CENTER
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        // 设置边框
        style.setBorderTop(BorderStyle.THIN);
        return style;
    }
}
